package com.pengpai.pangucms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pengpai.pangucms.DTO.CurrentUser;
import com.pengpai.pangucms.entity.SysUser;
import com.pengpai.pangucms.mapper.SysUserMapper;
import com.pengpai.pangucms.service.ISysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pengpai.pangucms.utils.PageDomain;
import com.pengpai.pangucms.utils.UserUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 系统用户表 服务实现类
 * </p>
 *
 * @author lijunjie
 * @since 2023-07-30
 */
@Service
@Transactional
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {

    @Resource
    SysUserMapper sysUserMapper;

    @Override
    public Boolean updatePassword(String old_password, String new_password) {
        CurrentUser currentUser = UserUtils.CurrentUser();
        if(!UserUtils.matchs(old_password, currentUser.getPassword())){
            return false;
        }
        SysUser sysUser = currentUser.getSysUser().setPassword(UserUtils.encoder(new_password));
        sysUserMapper.updateById(sysUser);
        return true;
    }

    /**
     * Describe: 根据条件查询用户列表数据
     * Param: username
     * Return: 返回分页用户列表数据
     */
    @Override
    public PageInfo<SysUser> page(SysUser param, PageDomain pageDomain) {
        PageHelper.startPage(pageDomain.getPage(), pageDomain.getLimit());
        List<SysUser> sysUsers = list(new QueryWrapper<>(param));
        return new PageInfo<>(sysUsers);
    }
}
